const app = getApp()
var store = require("../../../utils/storageUtil.js")
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")
const util = require("../../../utils/util.js")

import WxValidate from '../../../utils/WxValidate'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeMsg: '获取验证码',
    disableGetCode: false,
    showModal: false,
    successBtn: '注册/登录',
    disabled: false,
    qiniu: constant.resource.qiniu
  },

  bindPickerChange: function (e) {
    this.setData({
      sitesIndex: e.detail.value,
      siteId: this.data.sites[e.detail.value].id
    })
  },

  initValidate: function () {
    // 验证字段的规则
    const rules = {
      name: {
        required: true,
      },
      gender: {
        required: true,
      },
      height: {
        required: true,
        number: true,
      },
      weight: {
        required: true,
        number: true,
      },
      fit: {
        required: true,
      },
      fitness_frequency: {
        required: true,
      }
    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      name: {
        required: '请输入姓名',
      },
      code: {
        required: '请输入验证码',
      }
    }

    this.WxValidate = new WxValidate(rules, messages)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      unionid: wx.getStorageSync(storage.keys.unionid)
    })

    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    wx.request({
      url: that.data.host + '/experience-sign-up/offline/index',
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          console.log(res.data.data.sites)
          that.setData({
            sites: res.data.data.sites
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getCode: function (e) {
    var that = this;
    if (!that.validMobile()) {
      return
    }

    that.setData({
      disableGetCode: true
    })

    var seconds = 60;
    var interval = setInterval(function (e) {
      seconds--;
      if (seconds < 0) {
        that.setData({
          disableGetCode: false,
          codeMsg: "重新获取验证码"
        })
        clearInterval(interval);
      } else {
        that.setData({
          codeMsg: "发送中(" + seconds + ")秒"
        })
      }
    }, 1000)

    wx.request({
      url: that.data.host + '/sms/verify-code?mobile=' + that.data.mobile,
      method: 'POST',
      success: function (res) {
        util.formatResponse(res, function (data) {
          that.setData({
            token: data.token
          })
        }, function (code, message) {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 1000
          })
          that.setData({
            disableGetCode: false,
            codeMsg: "获取验证码"
          })
          clearInterval(interval);
        })
      }
    })

  },

  formSubmit: function (e) {
    var that = this;
    that.setData({
      disabled: true
    })
    var data = e.detail.value;

    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 1000
      })
      that.setData({
        disabled: false
      })
      return false
    } else {
      if (!that.validMobile()) {
        that.setData({
          disabled: false
        })
        return
      }

      if (!that.data.code) {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none',
          duration: 1000
        })
        that.setData({
          disabled: false
        })
        return
      } else if (that.data.code.length != 6) {
        wx.showToast({
          title: '验证码不正确',
          icon: 'none',
          duration: 1000
        })
        that.setData({
          disabled: false
        })
        return
      }

      if (!that.data.siteId) {
        wx.showToast({
          title: '请选择门店',
          icon: 'none',
          duration: 1000
        })
        that.setData({
          disabled: false
        })
        return
      }

      wx.request({
        url: that.data.host + '/experience-sign-up/offline?token=' + that.data.token + '&code=' + that.data.code + '&siteId=' + that.data.siteId,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            that.setData({
              showModal: true,
              successBtn: that.data.unionid ? '返回' : '注册/登录'
            })
          } else {
            that.setData({
              disabled: false
            })
            wx.showToast({
              title: res.data.message ? res.data.message : '预约失败',
              icon: 'none',
              duration: 1000,
              mask: true
            })
          }
        },
        fail: function (res) {
          that.setData({
            disabled: false
          })
          wx.showToast({
            title: '预约线下体验失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      });
    }
  },

  validMobile: function () {
    var that = this;

    if (!that.data.mobile) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!/^((13)|(15)|(16)|(17)|(18))\d{9}$/.test(that.data.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    return true;
  },

  inputMobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  inputCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },

  onSuccess: function () {
    if(this.data.unionid) {
      wx.reLaunch({
        url: '../../malls/malls-index/malls-index'
      })
    } else {
      wx.reLaunch({
        url: '../../index/index'
      })
    }
  }
})